package com.zidian.teacher.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import com.zidian.teacher.R;
import com.zidian.teacher.bean.CourseListBean;


/**
 * Created by Administrator on 2016/8/24 0024.
 */
public class CourseListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<CourseListBean.DataBean> data;


    public CourseListAdapter(Context context, List<CourseListBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_course_list, parent,
                false);
        return new ItemViewHolder(view);
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {

            ((ItemViewHolder) holder).courseName.setText(data.get(position).getCourseName());
            ((ItemViewHolder) holder).courseClass.setText(data.get(position).getCourseClass());
            ((ItemViewHolder) holder).courseTime.setText(data.get(position).getCourseTime());

            if (onItemClickListener != null) {

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemClick(holder.itemView, position);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemLongClick(holder.itemView, position);
                        return false;
                    }
                });

            }
        }
    }


    static class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView courseName;
        TextView courseClass;
        TextView courseTime;

        public ItemViewHolder(View view) {
            super(view);
            courseClass = (TextView) view.findViewById(R.id.course_class);
            courseName = (TextView) view.findViewById(R.id.course_name);
            courseTime = (TextView) view.findViewById(R.id.course_time);
        }
    }


}
